<?php

require_once 'Zend/View/Helper/Placeholder/Container/Standalone.php';

class Myzen_View_Helper_JavascriptData  extends Zend_View_Helper_Placeholder_Container_Standalone {
    
	protected $_dataKeyValue = array();
	
	 /**
     * Constructor
     *
     * Set separator to PHP_EOL.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
        $this->setSeparator(PHP_EOL);
    }
	
	
    public function javascriptData($key = '',$value = '') {
		if(!empty($key)) $this->setKeyValue($key,$value);
		return $this;
	} //end javascriptData
	
	public function setKeyValue($key = '',$value = '') {
		if(empty($key)) {
			require_once('Zend/View/Exception.php');
			throw new Zend_View_Exception('JavascriptData key is empty');
		}
		if(preg_match('/[^a-zA-Z0-9_]/',$key)) {
			require_once('Zend/View/Exception.php');
			throw new Zend_View_Exception('only alphanumeric and _ for javascript Key');
		}
	
		if(!empty($key)) $this->_dataKeyValue[$key] = $value;	
	}
	
	
	public function toString()
    {
		if(empty($this->_dataKeyValue)) return '';
		$dataString = '';
		foreach($this->_dataKeyValue as $key => $value) {
			$dataString .= '<div class="javascriptData" id="js_'.$key.'">'.$this->view->escape($value).'</div>'."\n";
		}
		return $dataString;
	}
} //end class